package org.bigjava.novelplatform.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bigjava.novelplatform.common.utils.R;
import org.bigjava.novelplatform.common.utils.StringUtils;
import org.bigjava.novelplatform.common.verifyGroup.Register;
import org.bigjava.novelplatform.entity.Books;
import org.bigjava.novelplatform.entity.Catalog;
import org.bigjava.novelplatform.entity.Chapter;
import org.bigjava.novelplatform.entity.Users;
import org.bigjava.novelplatform.service.CommonService;
import org.bigjava.novelplatform.service.UserService;
import org.bigjava.novelplatform.service.WriterService;
import org.bigjava.novelplatform.vo.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.bigjava.novelplatform.common.constant.NovelInfoConstant.SESSION_USER;
import static org.bigjava.novelplatform.common.exception.ExceptionCodeMessage.*;


/**
 * @author ykh
 * @create 2022-03-22 19:37
 */
@Validated
@RestController
@RequestMapping("/api")
@Slf4j
public class CommonController {

    @Resource
    public CommonService commonService;

    @Resource
    public UserService userService;

    @Resource
    public WriterService writerService;


    /**
     * 发送注册码
     *
     * @param
     * @return
     */
    @PostMapping("/SendRegisterCode")
    public R sendRegisterCode(@NotEmpty(message="邮箱不能为空") @Email String email) {
            //发送邮箱
        if (userService.sendMailRegister(email)) {
            return R.ok();
        }
        return R.error(AUTH_CODE_FAIL);
    }


    /**
     * 注册
     * 流程：1发送注册码sendRegisterCode-->2注册register
     *
     * @param user
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody @Validated(Register.class) Users user,HttpSession session) {
        //昵称
        //密码
        //注册邮箱
        //验证码
        UserInfoVo register = userService.register(user);
        session.setAttribute(SESSION_USER,register);
        return R.ok().setData(register);
    }

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody @Validated Users user,HttpSession session) {

        //昵称
        //邮箱
        //密码
        UserInfoVo login = userService.login(user);
        if (login == null) {
            return R.error(USER_LOGIN);
        }
        log.debug("登录用户{}",login);
        session.setAttribute(SESSION_USER,login);
        return R.ok().setData(login);
    }


    /**
     * 查询建议
     *
     * @param input
     * @return
     */
    @GetMapping("/suggest")
    public R getSuggest(@RequestParam(value = "input", required = false) String input) {
        List<SearchBriefInfoVo> vo;
        if (StringUtils.isEmpty(input)) {
            vo = commonService.getSuggestVo();
        } else {
            vo = commonService.getSuggestVoByInput(input);
        }
        return R.ok().setData(vo);
    }

    //===================排行================================

    /**
     * 二级分类
     * @return
     */
    @GetMapping("/catalog/one")
    public R topBookList() {
        List<String> catalog = commonService.selectCatalogOne();
        return R.ok().setData(catalog);
    }
    /**
     * 根据不同榜单类型返回榜单前10
     * type榜单类型
     *
     * @return
     */
    @Deprecated
    @GetMapping("/topBookList/{type}")
    public R topBookList(@PathVariable("type") String type) {
        if (StringUtils.isEmpty(type)) {
            return R.error(UNSUPPORTED_URL);
        }
        List<Books> books = commonService.bookList(type, 10);
        return R.ok().setData(books);
    }

    /**
     * 获取全部排行得前10
     * @param catalog
     * @return
     */
    @GetMapping("/selectBookList")
    public R selectBookList(@RequestParam(value = "catalog",required = false)String catalog) {
        HashMap<String, List<BookOrderVo>> map = commonService.selectBookList(catalog, 10);
        return R.ok().setData(map);
    }

    /**
     * 排行分页
     * @param catalog
     * @param orderType
     * @param current
     * @param size
     * @return
     */
    @Deprecated
    @GetMapping("/bookList/order")
    public R orderPage(String catalog,String orderType, Integer current, Integer size) {
        Page<Books> ranking = commonService.ranking(catalog, orderType, current, size);
        return R.ok().setData(ranking);
    }

    /**
     * 新书热度排行 分页
     * @param catalog
     * @param orderType 限定条件
     * @param current
     * @param size
     * @return
     */
    @Deprecated
    @GetMapping("/bookList/order/newbook")
    public R orderNewBookPage(String catalog,String orderType, Integer current, Integer size) {
        Page<Books> booksPage = commonService.newBookRanking(catalog, orderType, current, size);
        return R.ok().setData(booksPage);
    }

    @GetMapping("/rankingPaging")
    public R rankingPaging(String catalog,String type, Integer current, Integer size){
        Page<BookOrderPageVo> bookOrderPageVoPage = commonService.selectBookListPage(catalog, type, current, size);
        return R.ok().setData(bookOrderPageVoPage);
    }


    /**
     * 组合条件分页查询
     * @param current
     * @param size
     * @param queryCriteriaplus
     * @return
     */
    @PostMapping("/bookList/{current}/{size}")
    public R bookList( @PathVariable("current") Integer current,
                      @PathVariable(value = "size",required = false) Integer size,@RequestBody QueryCriteriaPlus queryCriteriaplus) {
        System.out.println(queryCriteriaplus);
        Page<SimpleBookVo> page = commonService.bookListPage(queryCriteriaplus, current, size == null ? 20 : size);
        return R.ok().setData(page);
    }

    /**
     * 查询书籍详情信息
     * @param bookId
     * @return
     */
    @GetMapping("selectBook/{bookId}")
    public R getBookInfo(@PathVariable Integer bookId){
        BookInfoVo bookInfo = commonService.getBookInfo(bookId);
        return R.ok().setData(bookInfo);
    }

    /**
     * 获取作者信息
     * @param writerId
     * @return
     */
    @GetMapping("writerInfo/{id}")
    public  R getWriterInfo(@PathVariable("id") Integer writerId){
        WriterInfoVo writer = commonService.getWriterInfo(writerId);
        return R.ok().setData(writer);
    }

    /**
     * 获取作者所有的简略书籍信息
     * @param writerId
     * @return
     */
    @GetMapping("writerBooks/{id}")
    public  R getWriterBooks(@PathVariable("id") Integer writerId){
        List<BookInfoVo> booksList = commonService.getWriterBook(writerId);
        return R.ok().setData(booksList);
    }

    /**
     * 获取分层
     * @param level
     * @return
     */
    @GetMapping("catalog/{level}")
    public R getCatalogByLevel(@PathVariable("level") Integer level){
        List<Catalog> catalogs = commonService.getCatalogByLevel(level);
        return R.ok().setData(catalogs);
    }

    /**
     * 获取全部分类
     * @return
     */
    @GetMapping("catalog")
    public R getCatalogAll(){
        List<CatalogVo> catalogs = commonService.getCatalogAll();
        return R.ok().setData(catalogs);
    }

    /**
     * 获取书籍章节目录
     * @param bookId
     * @return
     */
    @GetMapping("/bookChapter/{bookId}")
    public R bookChapter(@PathVariable("bookId") Integer bookId){
        List<ChapterVo> chapterVos = commonService.getChapterContents(bookId);
        return R.ok().setData(chapterVos);
    }


    /**
     * 查询书籍部分信息
     * @param bookId
     * @return
     */
    @GetMapping("selectBookportion/{bookId}")
    public R getBookInfoportion(@PathVariable Integer bookId){
        BookInfoPortion bookInfoportion = commonService.getBookInfoportion(bookId);
        return R.ok().setData(bookInfoportion);
    }


    /**
     * 获取章节内容
     * @param bookId
     * @param chapterId
     * @return
     */
    @GetMapping("/selectChaptergain")
    public R selectChaptergain(Integer bookId, Long chapterId){
        String s = commonService.selectChapter(bookId, chapterId);
        return R.ok().setData(s);
    }

    @GetMapping("/selectChapter")
    public R selectChapter(Integer bookId, Long chapterId){
        Chapter c = commonService.selectChapterObj(bookId, chapterId);
        return R.ok().setData(c);
    }



    /**
     * 获取章节内容通过章节序数查询
     * @param bookId
     * @param chapterOrder
     * @return
     */
    @GetMapping("/selectChaptergainOrder")
    public R selectChaptergainOrder(Integer bookId, Integer chapterOrder){
        ChapterOrderVo chapterOrders = commonService.selectChapterOrder(bookId, chapterOrder);
//        System.out.println(chapterOrders);
        return R.ok().setData(chapterOrders);
    }




    /**
     * 通过章节id和段落序列id查询评论的用户
     * @param chapterId
     * @param paragraphLocation
     * @return
     */
    @GetMapping("/selectChapterSays/{chapterId}/{paragraphLocation}")
    public R selectChapterSays(@PathVariable Long chapterId,@PathVariable Integer paragraphLocation){
        List<UserchaptersaysVo> userchaptersaysVos = commonService.selectChapterSays(chapterId, paragraphLocation);
        return R.ok().setData(userchaptersaysVos);
    }


    /**
     * 通过章节id和父评论查询评论的用户
     * @param chapterId
     * @param conversationTargetId
     * @return
     */
    @GetMapping("/selectChapterSaysZipin/{chapterId}/{conversationTargetId}")
    public R selectChapterSaysZipin(@PathVariable Long chapterId,@PathVariable Integer conversationTargetId){
        List<UserchaptersaysVo> userchaptersaysVos = commonService.selectChapterSaysZipin(chapterId, conversationTargetId);
        return R.ok().setData(userchaptersaysVos);
    }



}
